package com.wzxy.ping.action;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class SuperController extends ActionSupport implements ModelDriven{

	private static final long serialVersionUID = 1L;

	@Override
	public Object getModel() {
		return null;
	}
	
	/**
	 * 获取request内置对象
	 * @return
	 * @throws Exception
	 */
	public HttpServletRequest getRequest() throws Exception {
		ActionContext ctx = ActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) ctx
				.get(ServletActionContext.HTTP_REQUEST);
		return request;
	}
	
	/**
	 * 获取response内置对象
	 * @return
	 * @throws Exception
	 */
	public HttpServletResponse getResponse() throws Exception {
		ActionContext ctx = ActionContext.getContext();
		HttpServletResponse request = (HttpServletResponse) ctx
				.get(ServletActionContext.HTTP_RESPONSE);
		return request;
	}

	/**
	 * 获取session内置对象
	 * @throws Exception
	 */
	public HttpSession getSession() throws Exception {
		ActionContext ctx = ActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) ctx
				.get(ServletActionContext.HTTP_REQUEST);
		HttpSession session = request.getSession();
		return session;
	}
	
	/**
	 * 获取ServletContext对象
	 * @throws Exception
	 */
	public ServletContext getServletContext() throws Exception {
		ActionContext ctx = ActionContext.getContext();
		ServletContext context = ServletActionContext.getServletContext();
		return context;
	}
	
	/**
	 * 获取请求参数
	 * @throws Exception
	 */
	protected Map<String, String> getParamMap() throws Exception {
		HttpServletRequest request = getRequest();
		Map<String, String> map = new HashMap<String, String>();
		Enumeration enu = request.getParameterNames();
		while(enu.hasMoreElements()) {
            String paraName = (String)enu.nextElement();   
            String paraValue = request.getParameter(paraName); 
            if(paraValue!=null && !"".equals(paraValue)){
            	map.put(paraName, paraValue);
            }           
		}	
		return map;
	}
}
